<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

function create_dirs($file =''){
    //$dir_paths = explode("/",str_replace("images",$this->cache_patch,$file));
    $dir_paths = explode("/",$file);
    $dir = "";
    for($i=0; $i<count($dir_paths) -1; $i++){
        $dir_path = $dir_paths[$i];
        $dir.= $dir_path."/";
        if (!is_dir($dir)) {
            mkdir($dir);
            chmod($dir, 0777);
        }
    }
    return $dir;    
}

function admin_url(){
	return base_url().'admin/';
}

function prd() {
	echo "<pre style='clear: both; border-top: 1px solid red;'>";
	if (func_get_args()) foreach (func_get_args() as $item) {
		print_r($item);
		echo "<hr />";
	}
	
	$bt = debug_backtrace();
	$caller = array_shift($bt);
	echo "<div style='color:red' >Call from line <strong>".$caller['line'] . "</strong> file " . $caller['file'] ."</div>";
	echo "</pre>";
	die();
}

function format_date($int_date, $date = '00:00:00 00/00/0000', $format = 'H:i:s d/m/Y') {
    if ($int_date && $int_date > 0)  {
        $date = date($format, $int_date);
    }
    return $date;
}


function post_url($post_id, $post_code = '') {
    $url = base_url() .$post_code.'-'.$post_id . ".html";
    return $url;
}

function admin_post_status($status) {
    $array = array(
        0 => '<span class="label label-info">Nháp</span>',
        1 => '<span class="label label-success">Xuất bản</span>',
    );
    return @$array[$status];
}

// rewiretURL tiếng việt
function Replace_TiengViet($str) {

    $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/i", 'a', $str);

    $str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/i", 'e', $str);

    $str = preg_replace("/(ì|í|ị|ỉ|ĩ)/i", 'i', $str);

    $str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/i", 'o', $str);

    $str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/i", 'u', $str);

    $str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/i", 'y', $str);

    $str = preg_replace("/(đ)/i", 'd', $str);

    
    $str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);

    $str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);

    $str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);

    $str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);

    $str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);

    $str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);

    $str = preg_replace("/(Đ)/", 'D', $str);

    // sử dụng cho ai có mục đích rewriteURL
    $str = str_replace(" ", "-", str_replace("&*#39;","",$str));

    return $str;
}

function tree_to_array($tree, &$out, $level = 0) {
    $t = clone($tree);
    if (isset($t->child)) {unset($t->child);}
    $a = (array)$t;
    $a['_level'] = $level;
    $out[] = $a;
    
    if (!empty($tree->child)) {
        foreach ($tree->child as $item) tree_to_array($item, $out, $level+1);
    }
}

/* End of file util_helper.php */
/* Location: ./application/helpers/util_helper.php */